package ru.isys.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;

import ru.isys.activity.MainActivity;

public class FileURIAndNsPanel extends JPanel implements HaveMainListener
{
    private String fileURI;
    private JLabel fileURILabel;
    private JTextField fileURITextField;
    private JLabel fileURIPostLabel;
    private JLabel soapNamespaceLabel;
    private JTextField soapNamespaceTextField;
    private JLabel soapNamespacePostLabel;
    private JButton runButton;
    private SpringLayout layout;
    private MainActivity listener;

    public FileURIAndNsPanel(String fileURI)
    {
        this.layout = new SpringLayout();
        this.setLayout(this.layout);
        this.fileURI = fileURI;
        prepareFileUri();
        prepareSoapNamespace();
          
        this.runButton = new JButton("Run");       
        runButton.addActionListener(new ActionListener()
        {
            
            @Override
            public void actionPerformed(ActionEvent e)
            {
                
                try
                {
                    fileURITextField.setVisible(false);
                    soapNamespaceTextField.setVisible(false);
                    fileURIPostLabel.setVisible(true);
                    fileURIPostLabel.setText(fileURITextField.getText());
                    soapNamespacePostLabel.setVisible(true);
                    soapNamespacePostLabel.setText(soapNamespaceTextField.getText());
                    
                    listener.onLoadFileAndNamespace(fileURITextField.getText(),soapNamespaceTextField.getText());
                    runButton.setVisible(false);
                }
                catch (TransformerConfigurationException e1)
                {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                catch (ParserConfigurationException e1)
                {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                
            }
        });
        add(runButton);  
        
        setLayoutConstrains();        
        this.validate();
    }

    private void prepareFileUri()
    {

        this.fileURILabel = new JLabel("File URI");        
        this.fileURITextField = new JTextField(this.fileURI);
        this.fileURIPostLabel= new JLabel();
        this.fileURIPostLabel.setVisible(false);
        this.add(fileURILabel);
        this.add(fileURITextField);
        this.add(fileURIPostLabel);
        
    
    }
    private void prepareSoapNamespace(){
        this.soapNamespaceLabel = new JLabel("NS for root tag");
        this.soapNamespaceTextField = new JTextField("http://");
        this.soapNamespacePostLabel = new JLabel();
        this.soapNamespacePostLabel.setVisible(false);
        this.add(soapNamespaceLabel);
        this.add(soapNamespaceTextField);
        this.add(soapNamespacePostLabel);
    }
    private void setLayoutConstrains(){
        layout.putConstraint(SpringLayout.WEST, fileURILabel, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.EAST, fileURITextField, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, fileURITextField, 4, SpringLayout.EAST, fileURILabel);
        layout.putConstraint(SpringLayout.NORTH, fileURITextField, 5, SpringLayout.NORTH, this);
        
        layout.putConstraint(SpringLayout.EAST, fileURIPostLabel, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, fileURIPostLabel, 7, SpringLayout.EAST, fileURILabel);
        layout.putConstraint(SpringLayout.NORTH, fileURIPostLabel, 8, SpringLayout.NORTH, this);
        
        layout.putConstraint(SpringLayout.NORTH, fileURILabel, 8, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, soapNamespaceLabel, 0, SpringLayout.WEST, fileURILabel);
        layout.putConstraint(SpringLayout.EAST, fileURILabel, 0, SpringLayout.EAST, soapNamespaceLabel);
        layout.putConstraint(SpringLayout.NORTH, soapNamespaceLabel, 15, SpringLayout.SOUTH, fileURILabel);
        layout.putConstraint(SpringLayout.NORTH, soapNamespaceTextField, 9, SpringLayout.SOUTH, fileURITextField);
        layout.putConstraint(SpringLayout.WEST,soapNamespaceTextField ,0, SpringLayout.WEST, fileURITextField);
        layout.putConstraint(SpringLayout.EAST,soapNamespaceTextField ,0, SpringLayout.EAST, fileURITextField);
        
        layout.putConstraint(SpringLayout.NORTH, soapNamespacePostLabel, 12, SpringLayout.SOUTH, fileURITextField);
        layout.putConstraint(SpringLayout.WEST,soapNamespacePostLabel ,3, SpringLayout.WEST, fileURITextField);
        layout.putConstraint(SpringLayout.EAST,soapNamespacePostLabel ,0, SpringLayout.EAST, fileURITextField);
                
        layout.putConstraint(SpringLayout.EAST,runButton ,0, SpringLayout.EAST, fileURITextField);
        layout.putConstraint(SpringLayout.NORTH,runButton ,5, SpringLayout.SOUTH, soapNamespaceTextField);
    }

    public String getFileURI()
    {
        return fileURI;
    }

    public void setFileURI(String fileURI)
    {
        this.fileURI = fileURI;
    }

    @Override
    public int getHeight()
    {
        return super.getHeight();
    }

    @Override
    public int getWidth()
    {
        return super.getWidth();
    }

    @Override
    public void setListener(MainActivity listener)
    {
        this.listener=listener;
    }

}
